package logic

import (
	"context"
	"goods/goods"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchCategoryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchCategoryLogic {
	return &SearchCategoryLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchCategoryLogic) SearchCategory(in *goods.ListCategoryRequest) (*goods.ListCategoryResponse, error) {
	// todo: add your logic here and delete this line
	res, err := l.svcCtx.Category.FindCategory(context.Background(), in.SearchMsg, in.Page, in.PageSize)
	if err != nil {
		return &goods.ListCategoryResponse{Code: -1, Msg: "获取商品分类列表失败"}, err
	}
	datas := []*goods.Category{}
	for i := 0; i < len(res); i++ {
		data := &goods.Category{
			Name:       res[i].Name,
			Sort:       int32(res[i].Sort),
			Status:     int32(res[i].Status.Int64),
			CreateTime: res[i].CreateTime.Time.Format("2006-01-02T15:04:05"),
			UpdateTime: res[i].UpdateTime.Time.Format("2006-01-02T15:04:05"),
		}
		datas = append(datas, data)
	}
	return &goods.ListCategoryResponse{Code: 200, Msg: "获取商品分类列表成功", Data: datas}, nil
}
